package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingDao orderSettingDao;
    @Override
    public void add(List<OrderSetting> orderSettingList) {
        if(orderSettingList!=null&&orderSettingList.size()>0){
            for (OrderSetting orderSetting : orderSettingList) {
                //查看日期是否存在，存在就执行修改操作，不存在就执行添加操作
                Integer count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
                if(count>0){
                    //该日期存在，执行修改操作
                    orderSettingDao.editNumberByOrderDate(orderSetting);
                }else {
                    //日期不存在则执行添加操作
                    orderSettingDao.add(orderSetting);
                }
            }
        }
    }

    @Override
    public List<Map<String,Object>> getOrderSettingByMonth(String date) {
        Map<String,String> map = new HashMap<>();
//        String begin = date+"-1";
//        String end = date+"-31";
        map.put("begin",date+"-1");
        map.put("end",date+"-31");
        List<OrderSetting> list = orderSettingDao.getOrderSettingByMonth(map);
        List<Map<String,Object>> result = new ArrayList<>();
        if(list!=null&&list.size()>0){
            for (OrderSetting orderSetting : list) {
                Map<String,Object> m = new HashMap<>();
                m.put("date",orderSetting.getOrderDate().getDate());
                m.put("number",orderSetting.getNumber());
                m.put("reservations",orderSetting.getReservations());
                result.add(m);
            }
        }
        return result;
    }

    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
        //判断该日期是否存在，如果存在执行修改操作，不存在执行添加操作
        Integer count = orderSettingDao.findCountByOrderDate(orderSetting.getOrderDate());
        if(count>0){
            //该值存在执行修改操作
            orderSettingDao.editNumberByOrderDate(orderSetting);
        }else{
            //执行添加操作
            orderSettingDao.add(orderSetting);
        }
    }
}
